/*  Novelties: A visual novel programming language based on JavaFX
    Copyright (C) 2008  Hao Xu
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * /
 /*
 * Dialog.fx
 *
 * Created on Apr 6, 2008, 2:48:24 PM
 */

package avgengine;

import javafx.ui.canvas.*;
import javafx.ui.*;
import java.lang.Thread;
import avgengine.layout.*;

/**
 * @author ertri
 */




class Dialog extends CompositeNode, Resizable, Translatable {
    attribute text: Group*;
    attribute group: Group;
    attribute cg:int;
    attribute buffer1:Group*;
    attribute buffer2:Group*;
    
    attribute noBG :boolean;
    attribute theight: int;
    attribute lspace: int;
    attribute background: Color;
    attribute borderColor:Color;
    attribute borderWidth:int;
    attribute arc:int;
    attribute speed:int;
    attribute dx:int;
    attribute dy:int;
    attribute pad:int*;
    attribute dopacity:Number;
    attribute layoutManager: LayoutManager;
    
    operation resize(x:int, y:int);
    //    operation appendPicture(para:PictureParagraph);
    operation appendText(para:GenericParagraph);
    operation clearTextAndDo(o:operation());
    operation clearText();
    operation replaceText(para:GenericParagraph);
    operation insertIntoCurrentBuffer(g:Group);
    operation clearCurrentBuffer();
    operation selectBuffer(i:int);
    operation getCurrentBuffer():Group*;
}

attribute Dialog.buffer1=[];
attribute Dialog.buffer2=[];
attribute Dialog.text = [Group{content:bind buffer1},Group{content:bind buffer2}];
attribute Dialog.dheight = 80;
attribute Dialog.dwidth = 300;
attribute Dialog.noBG = false;
attribute Dialog.theight = 0;
attribute Dialog.lspace = 5;
attribute Dialog.borderColor = purple;
attribute Dialog.borderWidth = 2;
attribute Dialog.arc = 10;
attribute Dialog.speed = 500;
attribute Dialog.dopacity = .5;
attribute Dialog.pad = [10,10];
attribute Dialog.cg = 0;
attribute Dialog.layoutManager = VerticalLayout{
    theight: bind theight,
    lspace: bind lspace
};

attribute Dialog.group =  Group{
    opacity: 1,
    transform: bind [translate(x,y)],
    content: [  Rect {
        opacity: bind dopacity
        x: bind dx
        y: bind dy
        height: bind dheight
        width: bind dwidth
        arcHeight: bind arc
        arcWidth: bind arc
        fill: bind background
        stroke: bind borderColor
        strokeWidth: bind borderWidth
        onMouseClicked: bind onMouseClicked
    } , Group {
        transform: [translate(pad[0],pad[1])],
        content:bind text
    }]
};

operation Dialog.composeNode(){
    return if not noBG then group else Group{
        opacity: 1,
        transform: bind [translate(x,y)],
        content: [  Group {
            transform: [translate(pad[0],pad[1])],
            content:bind text
        }]
    };
}
var anim = Animation{};


SLACK:int = 0;
operation Dialog.resize(x,y) {
    var anim = Animation{};
    anim.justResize(this, [x,y], speed);
}
operation Dialog.appendText(p) {
    
    p.setStage(this);
    var coor = this.layoutManager.add(p);
    var g = Group {
        opacity : 0
        transform: [translate(coor[0], coor[1])]
        content: p.getVisualNode()
    };
    
    insertIntoCurrentBuffer(g);
    
    var anim = Animation{};
    anim.justChangeOpacity(g, p.topacity, 100, speed);
    
}

operation Dialog.insertIntoCurrentBuffer(g:Group) {
    if(cg == 0) {
        insert g into buffer1;
    }else {
        insert g into buffer2;
    }
}

operation Dialog.changeBuffer() {
    if(cg == 0) {
        cg = 1;
    }else {
        cg = 0;
    }
}

operation Dialog.clearCurrentBuffer() {
    if(cg == 0) {
        delete buffer1;
    }else {
        delete buffer2;
    }
}

operation Dialog.getCurrentBuffer() {
    if(cg == 0) {
        return buffer1;
    }else {
        return buffer2;
    }
}


operation Dialog.clearTextAndDo(o) {
    var anim = Animation{};
    var buf = getCurrentBuffer();
    
    for(t in buf) {
        anim.justFadeOut(t, 100, speed);
    }
    changeBuffer();
    clearCurrentBuffer();
    layoutManager.clear();
    if(sizeof buf <> 0) {
        do{
            
            Thread.sleep(speed+SLACK:int);
            
        }
    }
    o();
}

operation Dialog.replaceText(p) {
    var anim = Animation{};
    var buf = getCurrentBuffer();
    
    for(t in buf) {
        anim.justFadeOut(t, 100, speed);
    }
    changeBuffer();
    clearCurrentBuffer();
    layoutManager.clear();
    var coor = layoutManager.add(p);
    var g = Group {
        opacity : 0
        transform: [translate(coor[0], coor[1])]
        content: p.getVisualNode()
    };
    
    insertIntoCurrentBuffer(g);
    anim.justChangeOpacity(g,p.topacity,100,speed);
    
}

operation Dialog.clearText() {
    var anim = Animation{};
    var buf = getCurrentBuffer();
    
    for(t in buf) {
        anim.justFadeOut(t, 100, speed);
    }
    
    changeBuffer();
    clearCurrentBuffer();
    layoutManager.clear();
}



//var dialog = Dialog{};
//Frame{content:
//    FlowPanel{
//        content:[
//        Canvas{content:dialog},
//        Button{
//            text:"Add Text",
//            action:operation() {
//                dialog.appendText(Paragraph{
//                    topacity:1
//                    text:"Paragraph"
//                    color:blue
//                    borderColor:green
//                    borderWidth:1
//                    font:Font{faceName: "Georgia", size: 30}
//                    height:30
//                });
//            }
//        },
//        Button{
//            var img = Image{url:"mak2.jpg"}
//            text:"Add Picture",
//            action:operation() {
//                dialog.appendText(PictureParagraph{
//                    topacity : 1
//                    image : img
//                    height : 100
//                });
//            }
//        },
//        Button{
//            text:"Clear",
//            action:operation() {
//                dialog.clearText();
//            }
//        },
//        Button{
//            text:"Move",
//            action:operation() {
//                dialog.x=10;
//                dialog.y=10;
//            }
//        }]
//    }
//    visible:true
//}